package com.example.attendancesystem.Service.Imp;

import com.example.attendancesystem.Mapper.*;
import com.example.attendancesystem.Models.*;
import com.example.attendancesystem.Service.PunchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PunchServiceImp implements PunchService {

    @Autowired
    private CheckTimeMapper checkTimeMapper;

    @Autowired
    private LeaveRowMapper leaveRowMapper;

    @Autowired
    private AftRowMapper aftRowMapper;

    @Autowired
    private MorRowMapper morRowMapper;

    @Autowired
    private EmployeesMapper employeesMapper;


    @Override
    public Boolean updateCheckTime(CheckTime updateCheckTime) {
        int res = checkTimeMapper.updateTime(updateCheckTime);
        return res == 1;
    }

    @Override
    public DataLeaveRow getAllLeaveRow() {
        DataLeaveRow dataLeaveRow = new DataLeaveRow();
        dataLeaveRow.setCode(0);
        dataLeaveRow.setMsg("");
        List<LeaveRow> leaveRows = leaveRowMapper.getAllLeaveRow();
        dataLeaveRow.setData(leaveRows);
        dataLeaveRow.setCount(leaveRows.size());
        return dataLeaveRow;
    }

    @Override
    public DataLeaveRow getLeaveRowByDate(String queryDate) {
        DataLeaveRow dataLeaveRow = new DataLeaveRow();
        dataLeaveRow.setCode(0);
        dataLeaveRow.setMsg("");
        List<LeaveRow> leaveRows = leaveRowMapper.getLeaveRowByDate(queryDate);
        dataLeaveRow.setData(leaveRows);
        dataLeaveRow.setCount(leaveRows.size());
        return dataLeaveRow;
    }

    @Override
    public DataMorRow getAllMorRow() {
        DataMorRow dataMorRow = new DataMorRow();
        dataMorRow.setCode(0);
        dataMorRow.setMsg("");
        List<MorRow> morRows = morRowMapper.getAllMorRow();
        dataMorRow.setData(morRows);
        dataMorRow.setCount(morRows.size());
        return dataMorRow;
    }

    @Override
    public DataMorRow getMorRowByDate(String queryDate) {
        DataMorRow dataMorRow = new DataMorRow();
        dataMorRow.setCode(0);
        dataMorRow.setMsg("");
        List<MorRow> morRows = morRowMapper.getMorRowByDate(queryDate);
        dataMorRow.setData(morRows);
        dataMorRow.setCount(morRows.size());
        return dataMorRow;
    }

    @Override
    public DataAftRow getAllAftRow() {
        DataAftRow dataAftRow = new DataAftRow();
        dataAftRow.setCode(0);
        dataAftRow.setMsg("");
        List<AftRow> aftRows = aftRowMapper.getAllAftRow();
        dataAftRow.setData(aftRows);
        dataAftRow.setCount(aftRows.size());
        return dataAftRow;
    }

    @Override
    public DataAftRow getAftRowByDate(String queryDate) {
        DataAftRow dataAftRow = new DataAftRow();
        dataAftRow.setCode(0);
        dataAftRow.setMsg("");
        List<AftRow> aftRows = aftRowMapper.getAftRowByDate(queryDate);
        dataAftRow.setData(aftRows);
        dataAftRow.setCount(aftRows.size());
        return dataAftRow;
    }

    @Override
    public DataEmp getMorAbsenceEmp(String queryDate) {
        //先查询所有的员工信息
        List<Employee> absenceEmp = new ArrayList<>();
        List<Employee> employees = employeesMapper.getAllEmpUseAbsence();
        //每个员工号查询是否有记录
        //没有就添加到缺勤列表中
        for (Employee employee : employees) {
            MorRow morRow;
            morRow = morRowMapper.getMorRowByEmpNumAndDate(employee.getEmpNumber(), queryDate);
            if (null == morRow) {
                absenceEmp.add(employee);
            }
        }
        //设置返回数据类型
        DataEmp dataEmp = new DataEmp();
        dataEmp.setCode(0);
        dataEmp.setMsg("");
        dataEmp.setData(absenceEmp);
        dataEmp.setCount(absenceEmp.size());
        return dataEmp;
    }

    @Override
    public DataEmp getAftAbsenceEmp(String queryDate) {
        //先查询所有的员工信息
        List<Employee> absenceEmp = new ArrayList<>();
        List<Employee> employees = employeesMapper.getAllEmpUseAbsence();
        //每个员工号查询是否有记录
        //没有就添加到缺勤列表中
        for (Employee employee : employees) {
            AftRow aftRow;
            aftRow = aftRowMapper.getAftRowByEmpNumAndDate(employee.getEmpNumber(), queryDate);
            if (null == aftRow) {
                absenceEmp.add(employee);
            }
        }
        //设置返回数据类型
        DataEmp dataEmp = new DataEmp();
        dataEmp.setCode(0);
        dataEmp.setMsg("");
        dataEmp.setData(absenceEmp);
        dataEmp.setCount(absenceEmp.size());
        return dataEmp;
    }


}
